<template>
  <div class="app-container">
    <!-- 高级查询 -->
    <el-form v-show="searchVisible" ref="queryForm" :model="queryParams" :inline="true">
      <el-form-item label="申请人基本要求:">
        <el-input v-model="queryParams.applicantQualification" size="small" placeholder="请输入申请人基本要求" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="开始资金额:">
        <el-input v-model="queryParams.creditLineFrom" size="small" placeholder="请输入开始资金额" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="月利息:">
        <el-input v-model="queryParams.montlyRate" size="small" placeholder="请输入月利息" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="审核天数:">
        <el-input v-model="queryParams.reviewDays" size="small" placeholder="请输入审核天数" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="风控费:">
        <el-input v-model="queryParams.riskManagementCharge" size="small" placeholder="请输入风控费" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="月管理费率:">
        <el-input v-model="queryParams.loanMonthlymanagementRate" size="small" placeholder="请输入月管理费率" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="产品名称:">
        <el-input v-model="queryParams.productName" size="small" placeholder="请输入产品名称" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="最高资金额:">
        <el-input v-model="queryParams.creditLineUpto" size="small" placeholder="请输入最高资金额" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="资方利息:">
        <el-input v-model="queryParams.rateForLender" size="small" placeholder="请输入资方利息" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="期限:">
        <el-input v-model="queryParams.productTerm" size="small" placeholder="请输入期限" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="还款期数:">
        <el-input v-model="queryParams.paybackNumbers" size="small" placeholder="请输入还款期数" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item>
        <el-button type="cyan" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <!-- 工具栏 -->
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button v-hasPermission="['loanProduct:finLoanProduct:insert']" type="primary" icon="el-icon-plus" size="mini" @click="handleAdd">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button v-hasPermission="['loanProduct:finLoanProduct:update']" type="success" icon="el-icon-edit" size="mini" :disabled="isSingle" @click="handleEdit">修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button v-hasPermission="['loanProduct:finLoanProduct:delete']" type="danger" icon="el-icon-delete" size="mini" :disabled="isMultiple" @click="handleRemove">删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button v-hasPermission="['loanProduct:finLoanProduct:export']" type="warning" icon="el-icon-download" size="mini" @click="handleExport">导出</el-button>
      </el-col>
      <right-toolbar :show-search.sync="searchVisible" @queryTable="handleQuery" />
    </el-row>

    <!-- 数据列表 -->
    <el-table v-loading="loading" :data="page.records" highlight-current-row @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="" align="center" prop="id" />
      <el-table-column label="创建时间" align="center" prop="createTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="申请人基本要求" align="center" prop="applicantQualification" />
      <el-table-column label="开始资金额" align="center" prop="creditLineFrom" />
      <el-table-column label="月利息" align="center" prop="montlyRate" />
      <el-table-column label="审核天数" align="center" prop="reviewDays" />
      <el-table-column label="付款类型" align="center" prop="paymentType" />
      <el-table-column label="风控费" align="center" prop="riskManagementCharge" />
      <el-table-column label="月管理费率" align="center" prop="loanMonthlymanagementRate" />
      <el-table-column label="产品名称" align="center" prop="productName" />
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="产品类型：采亦融等" align="center" prop="productType" />
      <el-table-column label="最高资金额" align="center" prop="creditLineUpto" />
      <el-table-column label="资方利息" align="center" prop="rateForLender" />
      <el-table-column label="期限" align="center" prop="productTerm" />
      <el-table-column label="还款期数" align="center" prop="paybackNumbers" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button v-hasPermission="['loanProduct:finLoanProduct:update']" size="mini" type="text" icon="el-icon-edit" @click="handleEdit(scope.row)">修改</el-button>
          <el-button v-hasPermission="['loanProduct:finLoanProduct:delete']" size="mini" type="text" icon="el-icon-delete" @click="handleRemove(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页 -->
    <pagination v-show="page.total>0" :total="page.total" :page.sync="page.current" :limit.sync="page.pageSize" @pagination="handleQuery" />

    <!-- 添加/修改对话框 -->
    <el-dialog :title="finLoanProductFormDialog.title" :visible.sync="finLoanProductFormDialog.visible" :close-on-click-modal="false" :before-close="resetFinLoanProductFormDialog" width="500px" append-to-body>
      <el-form ref="finLoanProductForm" :model="finLoanProductFormData" :rules="finLoanProductFormRules" label-width="80px">
        <el-form-item label="申请人基本要求" prop="applicantQualification">
          <el-input v-model="finLoanProductFormData.applicantQualification" placeholder="请输入申请人基本要求" />
        </el-form-item>
        <el-form-item label="开始资金额" prop="creditLineFrom">
          <el-input v-model="finLoanProductFormData.creditLineFrom" placeholder="请输入开始资金额" />
        </el-form-item>
        <el-form-item label="月利息" prop="montlyRate">
          <el-input v-model="finLoanProductFormData.montlyRate" placeholder="请输入月利息" />
        </el-form-item>
        <el-form-item label="审核天数" prop="reviewDays">
          <el-input v-model="finLoanProductFormData.reviewDays" placeholder="请输入审核天数" />
        </el-form-item>
        <el-form-item label="风控费" prop="riskManagementCharge">
          <el-input v-model="finLoanProductFormData.riskManagementCharge" placeholder="请输入风控费" />
        </el-form-item>
        <el-form-item label="月管理费率" prop="loanMonthlymanagementRate">
          <el-input v-model="finLoanProductFormData.loanMonthlymanagementRate" placeholder="请输入月管理费率" />
        </el-form-item>
        <el-form-item label="产品名称" prop="productName">
          <el-input v-model="finLoanProductFormData.productName" placeholder="请输入产品名称" />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="finLoanProductFormData.remark" placeholder="请输入备注" />
        </el-form-item>
        <el-form-item label="最高资金额" prop="creditLineUpto">
          <el-input v-model="finLoanProductFormData.creditLineUpto" placeholder="请输入最高资金额" />
        </el-form-item>
        <el-form-item label="资方利息" prop="rateForLender">
          <el-input v-model="finLoanProductFormData.rateForLender" placeholder="请输入资方利息" />
        </el-form-item>
        <el-form-item label="期限" prop="productTerm">
          <el-input v-model="finLoanProductFormData.productTerm" placeholder="请输入期限" />
        </el-form-item>
        <el-form-item label="还款期数" prop="paybackNumbers">
          <el-input v-model="finLoanProductFormData.paybackNumbers" placeholder="请输入还款期数" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="confirmFinLoanProductFormDialog">确 定</el-button>
        <el-button @click="resetFinLoanProductFormDialog">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import FinLoanProductApi from '@/api/loanProduct/FinLoanProductApi'

const defaultFinLoanProductFormData = {
  applicantQualification: null,  creditLineFrom: null,  montlyRate: null,  reviewDays: null,  paymentType: null,  riskManagementCharge: null,  loanMonthlymanagementRate: null,  productName: null,  productType: null,  creditLineUpto: null,  rateForLender: null,  productTerm: null,  paybackNumbers: null}
export default {
  name: 'FinLoanProductList',
  data() {
    return {
      options: {
        sysYnTypeOptions: this.$constant.sysYnTypeOptions
      },
      queryParams: {
        applicantQualification: null,
        creditLineFrom: null,
        montlyRate: null,
        reviewDays: null,
        paymentType: null,
        riskManagementCharge: null,
        loanMonthlymanagementRate: null,
        productName: null,
        productType: null,
        creditLineUpto: null,
        rateForLender: null,
        productTerm: null,
        paybackNumbers: null,
        isValid: this.$constant.Y
      },
      page: {
        current: 1,
        pageSize: 10,
        total: 0,
        records: []
      },
      finLoanProductFormDialog: {
        title: '',
        visible: false
      },
      finLoanProductFormData: Object.assign({}, defaultFinLoanProductFormData),
      finLoanProductFormRules: {
      },
      loading: true,
      searchVisible: true,
      multipleSelection: [],
      isSingle: true,
      isMultiple: true
    }
  },
  created() {
    this.handleQuery()
    this.loadOptions()
  },
  methods: {
    loadOptions() {
    },
    handleAdd() {
      this.openFinLoanProductFormDialog()
    },
    handleRemove(row) {
      const ids = row.id || this.multipleSelection.map(item => item.id)
      this.$confirm('是否确认删除数据?', '警告', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(function() {
        return FinLoanProductApi.remove(ids)
      }).then(() => {
        this.handleQuery()
        this.$message.success('删除成功')
      })
    },
    handleEdit(row) {
      const id = row.id || this.multipleSelection.map(item => item.id)[0]
      this.openFinLoanProductFormDialog(id)
    },
    handleQuery() {
      this.loading = true
      FinLoanProductApi.page(this.queryParams, this.page).then(response => {
        this.page.records = response.data.records
        this.page.total = response.data.total
        this.loading = false
      }, () => {
        this.loading = false
      })
    },
    handleExport() {
      this.$confirm('是否确认导出数据?', '警告', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(function() {
        return FinLoanProductApi.exportData(this.queryParams)
      }).then(response => {
        this.$common.download(response.msg)
      })
    },
    openFinLoanProductFormDialog(id) {
      this.finLoanProductFormDialog.visible = true
      if (id) {
        this.finLoanProductFormDialog.title = '融资产品-修改'
        FinLoanProductApi.get(id).then(response => {
          this.finLoanProductFormData = {
            ...this.finLoanProductFormData,
            ...response.data
          }
        })
      } else {
        this.finLoanProductFormDialog.title = '融资产品-添加'
      }
    },
    confirmFinLoanProductFormDialog() {
      this.$refs['finLoanProductForm'].validate(valid => {
        if (valid) {
          if (this.finLoanProductFormData.id) {
            FinLoanProductApi.edit(this.finLoanProductFormData).then(response => {
              this.$message.success('修改成功')
              this.resetFinLoanProductFormDialog()
              this.handleQuery()
            })
          } else {
            FinLoanProductApi.add(this.finLoanProductFormData).then(response => {
              this.$message.success('添加成功')
              this.resetFinLoanProductFormDialog()
              this.handleQuery()
            })
          }
        }
      })
    },
    resetFinLoanProductFormDialog() {
      this.finLoanProductFormDialog = {
        title: '',
        visible: false
      }
      this.finLoanProductFormData = Object.assign({}, defaultFinLoanProductFormData)
      this.resetForm('finLoanProductForm')
    },
    handleSelectionChange(selection) {
      this.multipleSelection = selection
      this.isSingle = selection.length !== 1
      this.isMultiple = !selection.length
    },
    resetQuery() {
      this.page.current = 1
      this.resetForm('queryForm')
      this.handleQuery()
    }
  }
}
</script>
